package com.jz.community.moduleshopping.shopCart.presenter;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.moduleshopping.shopCart.bean.CartPostageInfo;
import com.jz.community.moduleshopping.shopCart.bean.CartShopInfo;
import com.jz.community.moduleshopping.shopCart.bean.NewCartListInfo;
import com.jz.community.moduleshopping.shopCart.model.ShopCartModel;
import com.jz.community.moduleshopping.shopCart.ui.ShoppingCartView;

import java.util.List;

/**
 * Created by Tony.Fan on 2018/5/18 14:33
 */
public class ShopCartPresenter {

    private ShopCartModel shopCartModel;

    private ShoppingCartView shoppingCartView;

    public ShopCartPresenter(ShoppingCartView shoppingCartView) {
        this.shoppingCartView = shoppingCartView;
        shopCartModel = new ShopCartModel(shoppingCartView.getContext());
    }

    /**
     * 购物车基础数据
     *
     * @param newCartListInfo
     */
    public void addShopNewCartListInfo(NewCartListInfo newCartListInfo) {
        shopCartModel.setNewCartListInfo(newCartListInfo);
    }

    /**
     * 刷新购物车基础数据
     *
     * @param newCartListInfo
     */
    public void addShopNewRefreshCartListInfo(NewCartListInfo newCartListInfo) {
        shopCartModel.setNewRefreshCartListInfo(newCartListInfo);
        updateCartAmount();
    }

    /**
     * 购物车店铺优惠活动
     *
     * @param cartPostageInfo
     */
    public void addShopPostageInfo(CartPostageInfo cartPostageInfo) {
        shopCartModel.setCartPostageInfo(cartPostageInfo);
    }

    /**
     * 更新全部选中、与 否
     *
     * @param isAllSelect
     */
    public void changeShopSelect(boolean isAllSelect) {
        shopCartModel.setAllShopChk(isAllSelect);
        updateCartAmount();
    }

    /**
     * 刷新选中店铺集合
     *
     * @return
     */
    public List<CartShopInfo> getRefreshCartListInfo() {
        return shopCartModel.getRefreshCartListInfo();
    }

    /**
     * 更新店铺所有商品选中与否
     *
     * @param isChk
     * @param currentPos
     */
    public void updateShopAllGoodsItemChk(boolean isChk, int currentPos, OnLoadListener<List<CartShopInfo>> onLoadListener) {
        shopCartModel.setShopAllGoodsChk(isChk, currentPos, new OnLoadListener<List<CartShopInfo>>() {
            @Override
            public void onSuccess(List<CartShopInfo> cartShopInfos) {
                onLoadListener.onSuccess(cartShopInfos);
            }

            @Override
            public void onFail(String msg, int code) {

            }
        });
        updateCartAmount();
    }

    /**
     * 更新某个商品选中与否
     *
     * @param itemPos
     * @param isChk
     * @param currentPos
     */
    public void updateGoodsItemChk(int itemPos, boolean isChk, int currentPos, OnLoadListener<List<CartShopInfo>> onLoadListener) {
        shopCartModel.setGoodsItemChk(itemPos, isChk, currentPos, new OnLoadListener<List<CartShopInfo>>() {
            @Override
            public void onSuccess(List<CartShopInfo> cartShopInfos) {
                onLoadListener.onSuccess(cartShopInfos);
            }

            @Override
            public void onFail(String msg, int code) {

            }
        });
        updateCartAmount();
    }

    /**
     * 更新Item购买数量
     *
     * @param position
     * @param num
     */
    public void changeRecycleViewItemNum(int position, int num, int currentPos) {
        shopCartModel.changeRecycleViewItemNum(position, num, currentPos);
        if (shopCartModel.hasLeastOneChoose()) {
            updateCartAmount();
        }
    }

    /**
     * 更新 总金额和总优惠价
     */
    public void updateCartAmount() {
        boolean isSelectAll = shopCartModel.isAllSelectToPay();
        boolean isOneChooseBuy = shopCartModel.hasLeastOneChoose();
        int totalNum = shopCartModel.calculateTotalSelectedNum();
        double totalPrice = shopCartModel.calculateTotalSelectedPrice();
        double postagePrice = shopCartModel.calculateTotalSelectedPostagePrice();
        shoppingCartView.showShoppingCartBuyInfo(totalPrice, totalNum, isSelectAll, isOneChooseBuy, postagePrice);
    }

    /**
     * 更新购物车购买数量
     */
    public void updateCartShopBuyNum() {
        int totalShopNum = shopCartModel.calculateTotalAddNum();
        shoppingCartView.showShoppingCartBuyNum(totalShopNum);
    }

    /**
     * 结算商品弹框
     */
    public void changeBalanceSelectedGoods() {
        shopCartModel.changeBalanceSelectedGoods();
    }

    /**
     * 删除有效选中购物商品
     */
    public void deleteCartGoodsInfo() {
        shopCartModel.deleteCartGoodsInfo(new OnLoadListener<BaseResponseInfo>() {
            @Override
            public void onSuccess(BaseResponseInfo baseResponseInfo) {
                shoppingCartView.showDeleteShopingInfo(baseResponseInfo);
            }

            @Override
            public void onFail(String s, int code) {
            }
        });
    }

    /**
     * 删除失效选中购物商品
     */
    public void deleteCartInvalidGoodsInfo(long[] ids) {
        shopCartModel.deleteCartInvalidGoodsInfo(ids, new OnLoadListener<BaseResponseInfo>() {
            @Override
            public void onSuccess(BaseResponseInfo baseResponseInfo) {
                shoppingCartView.showDeleteShopingInfo(baseResponseInfo);
            }

            @Override
            public void onFail(String s, int code) {
            }
        });
    }
}
